/**
 * Created by WYH on 17/4/18.
 */
angular.module('tribalc').factory('Check', ['Prompt','Alert' ,function (Prompt,Alert) {
    function username(value, isNeedAlert) {
        var check = value && value.length >= 4 && value.length <= 20;
        if (isNeedAlert && !check) Alert.alertBox(Prompt.usernameError);
        return check;
    }

    function password(value, isNeedAlert) {
        var check = value && value.length >= 6 && value.length <= 20;
        if (isNeedAlert && !check) Alert.alertBox(Prompt.passwordError);
        return check;
    }

    function repassword(password, repassword, isNeedAlert) {
        var check = password == repassword;
        if (isNeedAlert && !check) Alert.alertBox(Prompt.passwordDifference);
        return check;
    }

    function companyName(value, isNeedAlert) {
        var check = value && value.length >= 4 && value.length <= 40;
        if (isNeedAlert && !check) Alert.alertBox(Prompt.companyNameError);
        return check;
    }

    function companyAddress(value, isNeedAlert) {
        var check = value && value.length >= 4 && value.length <= 50;
        if (isNeedAlert && !check) Alert.alertBox(Prompt.companyAddressError);
        return check;
    }

    function contactsName(value, isNeedAlert) {
        var check = (value && value.length >= 2 && value.length <= 20 && checkIsChineseOrEnglish(value));
        if (isNeedAlert && !check) Alert.alertBox(Prompt.contactsNameError);
        return check;
    }

    function phone(value, isNeedAlert) {
        var pattern = /(((400)-(\d{3})-(\d{4}))|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{3,7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)$/;
        var check = !pattern || pattern.test(value);
        if (isNeedAlert && !check) Alert.alertBox(Prompt.fixedTelephoneError);
        return check;
    }

    function mobilePhone(value, isNeedAlert) {
        var pattern = /^1[3|4|5|7|8][0-9]{9}$/;
        var check = pattern.test(value);
        if (isNeedAlert && !check) Alert.alertBox(Prompt.mobilePhoneError);
        return check;
    }

    function email(value, isNeedAlert) {
        var pattern = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
        var check = !value || pattern.test(value);
        if(isNeedAlert && !check) Alert.alertBox(Prompt.emailError);
        return check;
    }




    function checkIsChineseOrEnglish(str) {
        if (!str) return false;
        for(var i = 0; i < str.length; i++) {
            if (!str[i].match("[a-zA-Z]") && !str[i].match("[\u4e00-\u9fa5]")) {
                return false
            }
        }
        return true;
    }


    function idNo(idNoStr) {
        var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        return reg.test(idNoStr);
    }


    function doorCarNum(doorCardNumStr) {
        if (!doorCardNumStr) return false;
        if (doorCardNumStr.length != 8) return false;
        for(var i = 0; i < doorCardNumStr.length; i++) {
            var upperNumReg = /^[A-F]+$/;
            var number=/^[0-9]+$/;
            if (!number.test(doorCardNumStr[i]) && !upperNumReg.test(doorCardNumStr[i])) {
                return false;
            }
        }
        return true;
    }

    function staffName(staffNameStr) {
        return (checkIsChineseOrEnglish(staffNameStr) && staffNameStr.length >= 2 && staffNameStr.length <= 20)
    }


    function desc(desc){
        if (!desc || (desc.length<=100&& desc.length>=10)){
            return true
        }
        return false
    }
    function prodesc(desc){
        if (desc&& desc.length<=100&& desc.length>=10){
            return true
        }
        return false
    }




    function company(company) {
        return (
            username(company.name, true)
            && password(company.password, true)
            && repassword(company.password, company.repassword, true)
            && companyName(company.companyName, true)
            && companyAddress(company.address, true)
            && contactsName(company.linkman, true)
            && phone(company.phone, true)
            && mobilePhone(company.mobilePhone, true)
            );
    }


    function companyDetail(company) {
        return {

        }
    }


    return {
        /* 公司信息验证 */
        company: company,

        username: username,
        password: password,
        repassword: repassword,
        companyName:companyName,
        companyAddress: companyAddress,
        contactsName: contactsName,
        phone: phone,
        mobilePhone: mobilePhone,
        email: email,

        doorCarNum: doorCarNum,
        idNo:idNo,
        staffName:staffName,
        desc: desc,
        prodesc:prodesc
    }

}]);